package com.example.unisys.common.config;

import com.example.unisys.common.property.SwaggerProperty;
import com.example.unisys.common.property.UnisysProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Collections;

@Configuration
@EnableSwagger2
public class UnisysConfig {
    @Autowired
    private UnisysProperty property;

    @Bean
    public Docket swaggerApi() {
        SwaggerProperty swagger = property.getSwagger();
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage(swagger.getBasePackage()))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo(swagger));
    }

    private ApiInfo apiInfo(SwaggerProperty swagger) {
        return new ApiInfoBuilder().title(swagger.getTitle())
                .description(swagger.getDescription())
                .version(swagger.getVersion())
                .contact( new Contact(swagger.getAuthor(), swagger.getUrl(), swagger.getEmail()))
                .license( swagger.getLicense())
                .licenseUrl(swagger.getLicenseUrl())
                .extensions(Collections.emptyList()).build();
    }
}
